<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\AreaTab;
use App\Admin\Renderable\JobPositionTab;
use App\Admin\Repositories\Banner;
use App\Models\BannerModel;
use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use App\Services\Banner\BannerService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BannerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Banner(), function (Grid $grid) {
            $grid->model()->where('genre', BannerModel::GENRE_INDEX)->orderBy('sort');
            $grid->column('id');
            $grid->column('name');
            $grid->column('sort')->editable();
            $grid->column('pic')->image();
            $grid->column('parameters')->display(function ($parameters) {
                if ($this->cur_type == BannerModel::TYPE_POSITION_DETAILS){
                    $name = JobpositionModel::query()->where('id',$this->parameters)->value('position_name');
                    $parameters = "职位：$name";
                }elseif($this->cur_type == BannerModel::TYPE_SPECIAL_SECTION){
                    $name = PlazaModel::query()->where('id',$this->parameters)->value('name');
                    $parameters = "专区：$name";
                }
                return $parameters;
            });
            $grid->column('cur_type')->using(BannerModel::TYPE_LIST)->label([
                BannerModel::TYPE_URL => 'blue1',
                BannerModel::TYPE_POSITION_DETAILS => 'orange2',
                BannerModel::TYPE_SPECIAL_SECTION => 'orange'
            ]);
            $grid->column('cur_status')->switch();
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
//                $filter->equal('genre', )->width(2);
            });
//            $grid->order->orderable(); // 开启排序功能
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            // 禁用显示按钮
            $grid->disableViewButton();
            $grid->disableRowSelector();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Banner(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->hidden('genre')->value(BannerModel::GENRE_INDEX);
//            $form->number('sort')->default(10);
            $form->image('pic')->uniqueName()->autoUpload()->required()->removable(false)->saveFullUrl();
            $form->select('cur_type')->options(BannerModel::TYPE_LIST)->default(BannerModel::TYPE_URL)->required()
                ->when(BannerModel::TYPE_URL, function (Form $form) {
                    $form->text('parameters_url', '外链')->rules(['required_if:cur_type,'.BannerModel::TYPE_URL])
                        ->customFormat(function () {
                            if ($this->cur_type == BannerModel::TYPE_URL) {
                                return $this->parameters;
                            }else{
                                return null;
                            }
                        });
                })->when(BannerModel::TYPE_POSITION_DETAILS, function (Form $form) {
                    $form->selectTable('job_position_id', '选择岗位')
                        ->title('岗位选择')
                        ->from(JobPositionTab::make()->payload(['notIds' => []]))
                        ->model(JobpositionModel::class, 'id', 'position_name')
                        ->rules(['required_if:cur_type,'.BannerModel::TYPE_POSITION_DETAILS])
                        ->customFormat(function () {
                            if ($this->cur_type == BannerModel::TYPE_POSITION_DETAILS) {
                                return $this->parameters;
                            }else{
                                return null;
                            }
                        });
                })->when(BannerModel::TYPE_SPECIAL_SECTION, function (Form $form) {
                    $form->selectTable('area', '选择专区')
                        ->title('专区选择')
                        ->from(AreaTab::make()->payload(['notIds' => []]))
                        ->model(PlazaModel::class, 'id', 'name')
                        ->rules(['required_if:cur_type,'.BannerModel::TYPE_SPECIAL_SECTION])
                        ->customFormat(function () {
                            if ($this->cur_type == BannerModel::TYPE_SPECIAL_SECTION) {
                                return $this->parameters;
                            }else{
                                return null;
                            }
                        });
                })->when(BannerModel::TYPE_NONE, function (Form $form) {
                    $form->hidden('parameters')->value('#');
                });
//            $form->text('parameters')->required();
            $form->switch('cur_status');
            $form->display('created_at');
            $form->display('updated_at');
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableCreatingCheck();
//            $form->ignore(['parameters_url','job_position_id','area']);
            $form->saving(function (Form $form) {
                $cur_type = $form->input('cur_type');
                if ($cur_type == BannerModel::TYPE_URL) {
                    $form->parameters = $form->input('parameters_url');
                }elseif ($cur_type == BannerModel::TYPE_POSITION_DETAILS) {
                    $form->parameters = $form->input('job_position_id');
                }elseif ($cur_type == BannerModel::TYPE_SPECIAL_SECTION) {
                    $form->parameters = $form->input('area');
                }else{
                    $form->parameters = '#';
                }
                $form->deleteInput(['parameters_url','job_position_id','area']);
            });

            $form->saved(function (Form $form) {
                BannerService::cacheWarehouse();//更新缓存
            });

        });
    }
}
